package model.classesDAO;

import java.util.List;

import javax.persistence.TypedQuery;

import model.classe.Noticia;
import model.interfaceDAO.INoticiaDAO;

public class NoticiaDAO extends GenericoDAO<Noticia> implements INoticiaDAO {

	@Override
	public void cadastrar(Noticia entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
			this.entityManager.persist(entidade);
			this.entityManager.getTransaction().commit();
		
	}

	@Override
	public void alterar(Noticia entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
			this.entityManager.persist(entidade);
			this.entityManager.getTransaction().commit();

	}
	
	@Override
	public void remover(Noticia entidade) {
		if (!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		this.entityManager.remove(entidade);
		this.entityManager.getTransaction().commit();
		
	}
	
	@Override
	public Noticia pesquisarID(Noticia entidade) {
		if(!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		TypedQuery<Noticia> q = this.entityManager.createQuery("SELECT n FROM Noticia n WHERE id = " + entidade.getId(), Noticia.class);
		Noticia noticia = q.getSingleResult();
		return noticia;
	}

	@Override
	public List<Noticia> pesquisarTodos() {
		if(!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		TypedQuery<Noticia> q = this.entityManager.createQuery("SELECT n FROM Noticia n", Noticia.class);
		List<Noticia> noticias = q.getResultList();
		return noticias;
	}

}
